﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Web.UI.HtmlControls;

public partial class Main_AjaUtils_Chatbar : System.Web.UI.Page
{
    /*page defaults*/
    OfficeOnlineUser currentUser;
    String username;

    private void AuthenticateUser()
    {
        try
        {
            currentUser = (OfficeOnlineUser)Session["CurrentUser"];
            
                //new OfficeOnlineUser(Session["CurrentUser"].ToString());
        }
        catch (Exception ex)
        {
            frmChatbar.InnerHtml = "<div>Cannot get current user</div>";//Response.Redirect("../../login.aspx?urlReferrer=" + Request.Url);
        }

        if (currentUser == null)
        {
            frmChatbar.InnerHtml = "<div>Current user is null</div>";//Response.Redirect("../../login.aspx?urlReferrer=" + Request.Url);
        }
    }
    /*end page defaults*/

    ChatRoom room = new ChatRoom();

    protected void Page_Load(object sender, EventArgs e)
    {
        AuthenticateUser();
        username = currentUser.UserName;
        String action = Request.QueryString["action"];
        if (action == "createroom")
        {
            String result = CreateRoom();
            if(result != "" && result != null)
                    frmChatbar.InnerHtml = "<response>Success</response>";
            else
                    frmChatbar.InnerHtml = "<response>Fail</response>";                              
        }        
        else
            GetUserChatRooms();
    }

    private String CreateRoom()
    {
        String creatorID = Request.QueryString["creatorID"].ToString();
        String targetID = Request.QueryString["targetID"];
        String roomName = Request.QueryString["roomName"];

        String roomID = room.Create(roomName,creatorID);
        int result1 = room.Join(creatorID);
        int result2 = room.Join(targetID);
        
        return roomID;
    }

    private void GetUserChatRooms()
    {
        DataTable data = new DataTable();
        //if (currentUser == null)
        //    frmChatbar.InnerHtml = "<div>Current user is null</div>";
        OfficeOnlineUser user = new OfficeOnlineUser(username);
        data = user.GetCurrentChatRooms();
        chatroomList.DataSource = data;
        chatroomList.DataBind();        
    }

    public void itemBound(object sender, RepeaterItemEventArgs e)
    {
        switch (e.Item.ItemType)
        {
            case ListItemType.Item:
            case ListItemType.AlternatingItem:
                HtmlContainerControl btn = (HtmlContainerControl)e.Item.FindControl("divWindow");
                btn.Attributes.Add("onclick", "showChatWindow('" + DataBinder.Eval(e.Item.DataItem, "roomID") + "','" +
                                    DataBinder.Eval(e.Item.DataItem, "roomName") + "','" + 
                                    e.Item.ItemIndex + "')");                
                //btn.Attributes.Add("onclick", "showChatWindow('" + e.Item.ItemIndex.ToString() + "')");                
                break;
        }
    }
}
